/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package protect01;

import java.util.*;

/**
 *
 * @author Constantin
 */
public class Dict {
    
    char[] al;
    int k;
    
    char[][] to;
    char[][] from;
    
    public Dict(char[] alfa, String keyWord, int k) {
 
        Set<Character> sc = new HashSet<>();
        for (int i = 0; i < alfa.length; i++) {
            sc.add(alfa[i]);
        }
        List<Character> lc = new ArrayList<>(sc);
        Collections.sort(lc);
        al = new char[lc.size()];
        for (int i = 0; i < lc.size(); i++)
            al[i] = lc.get(i);
        
        int size = al.length;
        
        //this.al = Arrays.copyOf(alfa, size);
        this.k = k;
        
        to = new char[2][];
        to[0] = Arrays.copyOf(al, size);
        to[1] = new char[size];
        
        int pos = k % size;
        
        Set<Character> ks = new HashSet<Character>();
        //for (int i = 0; i < keyWord.length(); i++)
            //ks.add(keyWord.charAt(i));
        ArrayList<Character> kl = new ArrayList<Character>();
        for (int i = 0; i < keyWord.length(); i++) {
            if (!ks.contains(keyWord.charAt(i))) {
                ks.add(keyWord.charAt(i));
                kl.add(keyWord.charAt(i));
            }
        }
        
        for (int i = 0; i < kl.size(); i++) {
            to[1][pos + i] = kl.get(i);
            lc.remove(kl.get(i));
            //System.out.print(to[1][pos + i] + " ");
        }
        for (int i = 0; i < size - kl.size(); i++) 
            to[1][(pos + kl.size() + i) % size] = lc.get(i);
        
        HashMap<Character, Character> hcc = new HashMap<Character, Character>();
        for (int i = 0; i < size; i++)
            hcc.put(to[1][i], to[0][i]);
        from = new char[2][];
        from[0] = new char[size];
        from[1] = new char[size];
        for (int i = 0; i < size; i++){
            from[0][i] = al[i];
            from[1][i] = hcc.get(al[i]);
        }
    }
    
    public char encode(char c) {
        int pos = Arrays.binarySearch(to[0], c);
        return (pos < 0 ? c : to[1][pos]);
    }
    
    public char decode(char c) {
        int pos = Arrays.binarySearch(from[0], c);
        return (pos < 0 ? c : from[1][pos]);
    }
    
    public char[] encodeAll(char[] c) {
        char[] result = new char[c.length];
        for (int i = 0; i < c.length; i++)
            result[i] = encode(c[i]);
        return result;
    }
    
    public char[] decodeAll(char[] c) {
        char[] result = new char[c.length];
        for (int i = 0; i < c.length; i++)
            result[i] = decode(c[i]);
        return result;
    }
    
    public String encodeString(String s) {
        String result = new String(encodeAll(s.toCharArray()));
        return result;
    }
    
    public String decodeString(String s) {
        String result = new String(decodeAll(s.toCharArray()));
        return result;
    }
    
    @Override
    public String toString() {
        String s = "";
        for (int i = 0; i < to[0].length; i++) {
            s = s.concat(String.format("%c ", to[0][i]));
        }
        s = s.concat("\n");
        for (int i = 0; i < to[0].length; i++) {
            s = s.concat(String.format("%c ", to[1][i]));
        }
        return s;
    }
}
